#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// This test checks scoreFor().

int main()
{
	int i, j, h;
	int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse, sea_hag, tribute, smithy};
	struct gameState G;
	int randomSeed;
	int r, m;
	int numPlayer = 4;
	int estateDeck[50];
	int numtests = 5;
	int expected;
	
	srand(time(NULL));
	randomSeed = (rand() % 10)+1;
	
	m = initializeGame(numPlayer, k, randomSeed, &G);
	printf("Game initialized.\n");
	
	// Player 0 has no score cards, score should be 0.
	G.handCount[0] = 0;
	G.discardCount[0] = 0;
	r = scoreFor(0, &G);
	printf("Expected score for Player 0: 0. Returned: %i. ", r);
	if(r == 0)
		printf("PASSED.\n");
	else
		printf("FAILED.\n");
		
	// Player 1 has no cards in hand or discard, but has a score of 50 in deck.
	G.handCount[1] = 0;
	G.discardCount[1] = 0;
	for(i = 0;i<50;i++)
		estateDeck[i] = 1;
	memcpy(G.deck[1], estateDeck, sizeof(int) * 50);
	G.deckCount[1] = 50;
	r = scoreFor(1, &G);
	printf("Expected score for Player 1: 50. Returned %i. ", r);
	if(r == 50)
		printf("PASSED.\n");
	else
		printf("FAILED!\n");
		
	// Player 2 has a random number of victory cards in their hand, deck, and discard.
	// Tested numtests times
	for(j=0;j<numtests;j++)
	{
			G.handCount[2] = rand() % 10;
			G.discardCount[2] = rand() % 30;
			G.deckCount[2] = rand() % 50;
		for(h=0;h<G.handCount[2];h++)
			G.hand[2][h] = 3;			// provinces
		for(h=0;h<G.discardCount[2];h++)
			G.discard[2][h] = 2;		// duchies
		for(h=0;h<G.deckCount[2];h++)
			G.deck[2][h] = 1;			// estates
		expected = G.handCount[2]*6+G.discardCount[2]*3 + G.deckCount[2];
		r = scoreFor(2, &G);
		printf("Expected (random) score for Player 2: %i. Returned %i. ",expected,r);
		if(expected == r)
			printf("PASSED.\n");
		else
			printf("FAILED!\n");	
	}
	
	// Player 3 has just 2 gardens and 100 copper. Check his score.
	G.handCount[3] = 2;
	G.discardCount[3] = 50;
	G.deckCount[3] = 50;
	for(h=0;h<G.discardCount[3];h++)
		G.discard[3][h] = 5;
	for(h=0;h<G.deckCount[3];h++)
		G.deck[3][h] = 5;
	G.hand[3][0] = 10;
	G.hand[3][1] = 10;
	r = scoreFor(3, &G);
	printf("Expected gardens score: 20. Returned %i. ", r);
	if(r == 20)
		printf("PASSED.\n");
	else
		printf("FAILED!\n");

	
	return 0;
}